package net.wanho.personalOffice.service.impl;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.service.impl.BaseServiceImpl;
import net.wanho.personalOffice.dao.IndividualTaskDAO;
import net.wanho.personalOffice.dao.TaskUserRelationsDAO;
import net.wanho.personalOffice.po.IndividualTask;
import net.wanho.personalOffice.po.TaskUserRelations;
import net.wanho.personalOffice.service.IndividualTaskService;
import net.wanho.systemSettings.po.Employee;

import java.util.ArrayList;
import java.util.List;


public class IndividualTaskServiceImpl extends BaseServiceImpl<IndividualTaskDAO, IndividualTask> implements IndividualTaskService {

    private TaskUserRelationsDAO taskUserRelationsDAO ;

    @Override
    public List<Employee> findEmployee() throws ServiceException {
        return super.baseDAO.selectEmployee() ;
    }

    /**
     * 执行两个表的插入，task和task user表，task user表为插入集合
     * @param entity
     * @return
     * @throws ServiceException
     */
    @Override
    public boolean save(IndividualTask entity) throws ServiceException {
        List<TaskUserRelations> taskUserRelationsList = new ArrayList<>();
        TaskUserRelations taskUserRelations = new TaskUserRelations();
        List<Employee> relationsUser = entity.getRelationsUsers();
        Integer taskId = super.baseDAO.insertGetId(entity);
        for (Employee employee : relationsUser) {
            taskUserRelations.setEmployeeId(employee.getEmployeeId());
            taskUserRelations.setTaskId(taskId);
            taskUserRelationsList.add(taskUserRelations);
        }
        taskUserRelationsDAO.insertBatch(taskUserRelationsList);
        return true;
    }

    @Override
    public boolean modify(IndividualTask entity) throws ServiceException {
        return super.baseDAO.update(entity);
    }

    @Override
    public boolean remove(String id) throws ServiceException {
        super.baseDAO.delete(id);
        taskUserRelationsDAO.delete(id);
        return true;
    }
}
